<template>
  <div>
    <div class="setting-container">
      <div class="app-container">
        <el-card>
          <!-- 新增角色按钮 -->
          <el-row style="height:60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addRole"
            >新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="list">
            <el-table-column
              label="序号"
              width="120"
              type="index"
            />
            <el-table-column
              label="角色名称"
              width="240"
              prop="name"
            />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <!-- <template #default="scope"> -->
              <template #default="scope">
                <el-button size="small" type="success" @click="assignPermission(scope.row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row type="flex" justify="center" align="middle" style="height: 60px">
            <!-- 分页组件 -->
            <el-pagination layout="prev,pager,next" :total="total" :page-size="params.pagesize" @current-change="pageChange" />
          </el-row>
        </el-card>
      </div>
      <!-- 新增弹框 -->
      <el-dialog :title="roleForm.id?'编辑':'新增角色'" :visible="showDialog" @close="dialodClose">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="dialodClose">取消</el-button>
            <el-button size="small" type="primary" @click.native="confirm">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 权限弹框 -->
      <AssignPermission :show-assign-dialog="showAssignDialog" :cur-id="roleId" @close-dialog="closeDialog" />
    </div>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting'
import AssignPermission from '@/views/setting/components/assign-permission.vue'

export default {
  name: 'Setting',
  components: {
    AssignPermission
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0, //  总条数
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      roleId: null, // 记录正在操作的角色id
      showAssignDialog: false // 控制弹层的显示隐藏

    }
  },
  mounted() {
    this.hGetRoleList()
  },
  methods: {
    async hGetRoleList() {
      const res = await getRoleList(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    pageChange(page) {
      this.params.page = page
      this.hGetRoleList()
    },
    // 删除
    delRole(id) {
      this.$confirm('是否确认删除该角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.list.length === 1 && this.params.page > 1 && this.params.page--
        this.hGetRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增角色按钮
    addRole() {
      this.showDialog = true
    },
    // 取消按钮
    dialodClose() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 添加角色
    confirm() {
      this.$refs.roleForm.validate(async(valid) => {
        if (valid) {
          if (this.roleForm.id) {
            // 编辑
            await updateRole(this.roleForm)
          } else {
            // 新增
            await addRole(this.roleForm)
          }
          this.hGetRoleList()
          this.dialodClose()
        }
      })
    },
    // 编辑按钮
    async editRole(id) {
      this.addRole()
      // 回填数据
      const res = await getRoleDetail(id)
      this.roleForm = res.data
    },
    assignPermission(id) {
      this.roleId = id
      this.showAssignDialog = true
    },
    closeDialog() {
      this.showAssignDialog = false
    }
  }
}
</script>

<style>

</style>
